﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Services;
using System.Web.UI;
using MichaelKappel.Net.CSharp;
using phudieuhungthinh.BL;
using phudieuhungthinh.BL.Model;

namespace phudieuhungthinh.Admin
{
    public partial class ProductDirectory : Page
    {
        private const string MenuKey = "AdminLeftMenu";
        private const int PageSize = 11;

        public int CategoryID
        {
            get
            {
                Int32 result = 0;
                return (Int32.TryParse(Request.QueryString["cateid"], out result)) ? result : 0;
            }
        }

        public int PageNum
        {
            get
            {
                Int32 result = 0;
                return (Int32.TryParse(Request.QueryString["pagenum"], out result)) ? result : 1;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                pnlCategory.Controls.Add(new LiteralControl(LayoutProducts()));

                var menuHtml = HelperCache.Get<string>(MenuKey);
                if (menuHtml == null)
                    lock (this)
                    {
                        if (menuHtml == null)
                        {
                            menuHtml = BuildMenu();
                            HelperCache.Add(MenuKey, menuHtml, HelperCache.CachingTime.ShortTermSliding);
                        }
                    }

                pnlMenuContainer.Controls.Add(new LiteralControl(menuHtml));
            }
        }

        private string BuildMenu()
        {
            var menuBuilder = new StringBuilder("<div class='end'>");
            var im = new CategoryImpl();
            List<CategoryMenu> menus = im.GetCategoryMenus();

            menuBuilder.Append("<ul>");
            foreach (CategoryMenu item in menus)
            {
                menuBuilder.AppendFormat("<li><a href='/Admin/ProductDirectory.aspx?cateid={1}'>{0}</a>", item.Name,
                    item.ID);
                if (item.SubMenus.Count > 0)
                {
                    menuBuilder.Append("<ul>");
                    foreach (CategoryMenu subItem in item.SubMenus)
                    {
                        menuBuilder.AppendFormat("<li><a href='/Admin/ProductDirectory.aspx?cateid={1}'>{0}</a></li>",
                            subItem.Name, subItem.ID);
                    }
                    menuBuilder.Append("</ul>");
                }
                menuBuilder.Append("</li>");
            }
            menuBuilder.Append("</ul>");
            menuBuilder.Append("</div>");

            return menuBuilder.ToString();
        }

        protected string LayoutProducts()
        {
            int rowCount = 0;
            var html = new StringBuilder();
            var im = new ProductImpl();
            List<ProductModel> items = im.PagingProducts(CategoryID, out rowCount, PageSize, PageNum);

            html.Append("<div id='product-list'>");
            html.Append("<div id='list'>");
            html.Append("<ul>");

            for (int i = 0; i < items.Count; i++)
            {
                if (i == 0 || i == 1)
                {
                    html.Append("<li class='item bigger'>");
                }
                else
                {
                    html.Append("<li class='item'>");
                }
                html.Append("<div class='img'>");
                if (items[i].ImageUrl != null)
                {
                    html.AppendFormat("<img src='/Images/Products/thumb/{0}' alt='{1}' />", items[i].ImageUrl,
                        items[i].Name);
                }
                html.Append("</div>");
                html.AppendFormat(
                    "<div class='roundedOne'><input type='checkbox' value='None' id='{0}' name='check' /><label for='{0}'></label></div>",
                    items[i].ID);
                html.AppendFormat("<div class='title'><a href='/Admin/ProductItem.aspx?id={0}'>{1}</a></div>",
                    items[i].ID, items[i].Name);
                html.AppendFormat("<div class='code'><span>{0}</span></div>", items[i].ProductNumber);
                html.Append("</li>");
            }
            html.Append("</ul>");
            html.Append("</div>");
            html.Append("<div class='nav1'>");
            if (PageNum > 1)
            {
                html.AppendFormat(
                    "<a class='prev' href='/Admin/ProductDirectory.aspx?cateid={0}&pagenum={1}'>Trở về</a>", CategoryID,
                    PageNum - 1);
            }
            if (PageNum*PageSize < rowCount)
            {
                html.AppendFormat(
                    "<a class='next' href='/Admin/ProductDirectory.aspx?cateid={0}&pagenum={1}'>Tiếp tục</a>",
                    CategoryID, PageNum + 1);
            }
            html.Append("</div>");
            html.Append("</div>");

            return html.ToString();
        }

        [WebMethod(EnableSession = true)]
        public static bool DeleteProduct(int productId)
        {
            try
            {
                //Delete a product in database.
                var productImpl = new ProductImpl();
                ProductModel record = productImpl.GetProductInfo(productId);
                productImpl.DeleteProduct(record);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}